package com.example.demo.lamada;


import org.junit.Test;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * 描述
 *
 * @author: yuy
 */
public class TestStream {

    public static void main(String[] args) {
        List<String> list = Arrays.asList("a", "b", "c", "d", "a", "a", "d", "d");

        Map<String, Integer> map = list.stream()
            .collect(Collectors.toMap(e -> e, e -> 1, (a, b) -> a + b));
        System.out.println(map);
    }


    @Test
    public void test1() {
        String str = "123,234,567,821,567,002";
        List<String> list = Arrays.stream(str.split(",")).distinct().collect(Collectors.toList());
        System.out.println(list);
    }

    @Test
    public void test2() {
        System.out.println("123470u2".split(",").toString());
    }

    @Test
    public void test3() {
        List<Integer> list = Arrays.asList(1, 4, 9, 8, 6, 11, 2);
        List<Integer> collect = list.stream().sorted().collect(Collectors.toList());
        System.out.println(collect);
    }

    @Test
    public void test44() {
        Integer[] nums = {1,2,1,3,4,3,5,4,7,7};
        Integer integer = Stream.of(nums).reduce((a, b) -> a ^ b).get();
        System.out.println(integer);

        List<Integer> data = Stream.of(nums)                             // list 对应的 Stream
            .collect(Collectors.toMap(e -> e, e -> 1, Integer::sum)) // 获得元素出现频率的 Map，键为元素，值为元素出现的次数
            .entrySet().stream()                   // 所有 entry 对应的 Stream
            .filter(entry -> entry.getValue() == 1) // 过滤出元素出现次数大于 1 的 entry
            .map(entry -> entry.getKey())          // 获得 entry 的键（重复元素）对应的 Stream
            .collect(Collectors.toList());// 转化为 List

        System.out.println(data);
    }

    @Test
    public void test4() {
        Integer[] nums = {1,2,1,3,4,3,5,4,7,7};
        Integer integer = Stream.of(nums).reduce((a, b) -> a ^ b).get();
        System.out.println(integer);

        List<Integer> data = Stream.of(nums)
            .collect(Collectors.toMap(e -> e, e -> 1, Integer::sum))
            .entrySet().stream()
            .filter(entry -> entry.getValue() == 1)
            .map(entry -> entry.getKey())
            .collect(Collectors.toList());

        System.out.println(data);
    }
}
